// pages/goods/search.js
var api = require('../../service/api.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        searching: true,
        recommendations: ['坚果仁', '核桃酥', '苹果', '彩色电视机'],
        search: {
            keyword: '',
            aCatList: '',
        },
        pagination: {
            pageIndex: 1,
            pageSize: 40
        },
        entities: []
    },
    quickSearch(e) {
        console.log('quickSearch', e);
        let keyword = e.target.dataset.keyword;
        this.setData({
            "search.keyword": keyword
        });
        this.search();
    },

    inputSearchText(e) {
        let keyword = e.detail.value;
        this.setData({
            "search.keyword": keyword
        });
    },
    search() {
        console.log(this.search);
        this.findProdBySearchCondition();
    },
    goodsDetail(event) {
        var goods = event.currentTarget.dataset.goods;
        wx.setStorageSync('goods', goods)
        wx.navigateTo({
            url: '/pages/goods/detail'
        });
        console.log(goods);
    },

    findProdBySearchCondition() {
        var
            that = this,
            search = this.data.search,
            pagination = this.data.pagination;
        var
            keyword = search.keyword || '',
            catAList = search.aCatList || undefined,
            bCatId = undefined,
            pageIndex = pagination.pageIndex,
            pageSize = pagination.pageSize;
        console.log('findProdBySearchCondition', search, pagination);
        api.findProdBySearchCondition(keyword, catAList, bCatId, pageIndex, pageSize).then(resp => {
            let rows = [];
            if (resp && resp.msg == 'SUCCESS') {
                resp.data.rows.map(item => {
                    if (item.prodImage) {
                        item.prodImages = item.prodImage.split(',');
                        item.firstProdImage = item.prodImages[0];
                    }
                });
                rows = resp.data.rows;
            }
            that.setData({
                entities: rows
            });
        }, resp => {
            wx.showToast({
                title: '服务器错误',
                icon: 'loading',
                duration: 2000
            })
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})